package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.AliyunVodSDKUtils;
import com.atguigu.vod.utils.ConstantPropertiesUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags="阿里云视频点播")
@RestController
@RequestMapping("/eduvod/vod")
@CrossOrigin
@Slf4j
public class VodController {
    @Autowired
    private VodService vodService;

    //上传视频
    @PostMapping("/upload")
    public R uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {

        String videoId = vodService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }

    @ApiOperation("根据id进行删除视频")
    @ApiImplicitParam(name = "id",value = "视频id",required =true)
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id){
        vodService.removeById(id);
        return R.ok();
    }

    @ApiOperation("批量删除视频")
//    @ApiImplicitParam(name = "videoIds",value = "视频id数组",required = true)
    @DeleteMapping("/removeVideiList")
    public R removeVideiList(@RequestParam("videoIds") List<String> videoIds){
        vodService.removeVideiList(videoIds);
        return R.ok();
    }

    @ApiOperation("获取视频播放凭证")
    @GetMapping("getplayauth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {

        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();
        log.info("videoId:"+videoId+"/t播放凭证:"+playAuth);
        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
